package com.csw;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TEST {
    public static void main(String[] args) throws Exception{
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://master:3306/csw";
        String username="root";
        String password="123456";
        Connection connection = DriverManager.getConnection(url, username, password);
        //查表
//        String sql="select * from student where sid=? and name=?";
//        PreparedStatement ps = connection.prepareStatement(sql);
//        ps.setString(1,"1011");
//        ps.setString(2,"test01");
//        ResultSet rs = ps.executeQuery();
//        while (rs.next()){
//            String name = rs.getString("name");
//            int age = rs.getInt("age");
//            String sex = rs.getString("sex");
//            System.out.println(name+"---"+age+"---"+sex);
//        }
//        rs.close();
//        ps.close();
//        connection.close();
        //删除数据
//        String sql="delete from student where sid=? and name=?";
//        PreparedStatement ps = connection.prepareStatement(sql);
//        ps.setString(1,"1011");
//        ps.setString(2,"test01");
//        int i = ps.executeUpdate();
//        System.out.println(i);
//        ps.close();
//        connection.close();
        //修改表数据
//        String sql="update student set sid=? , name=? where sid=1020";
//        PreparedStatement ps = connection.prepareStatement(sql);
//        ps.setString(1,"1050");
//        ps.setString(2,"csw123");
//        int i = ps.executeUpdate();
//        System.out.println(i);
//        ps.close();
//        connection.close();
        //添加数据
        String sql="insert into student(name,age,sex) values(?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1,"cswq");
        ps.setInt(2,25);
        ps.setString(3,"1");
        int i = ps.executeUpdate();
        System.out.println(i);
        ps.close();
        connection.close();
    }
}
